﻿using FarPoint.Web.Spread;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.Searching
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (IsPostBack)
        return;

      this.FpSpread1.UseClipboard = false;

      //Generate the dataset
      DataSet ds = new DataSet();
      string f = MapPath("..\\Freezing\\authors.xml");
      ds.ReadXml(f);

      //Load a skin
      //FarPoint.Web.Spread.DefaultSkins.GetAt(13).Apply(FpSpread1.Sheets[0]);
      //FpSpread1.CommandBar.BackColor = Color.Wheat;

      //Bind
      FpSpread1.DataSource = ds;

      //Create a Named Style for the search matches - define the appearance
      FarPoint.Web.Spread.NamedStyle sSearchMatch = new FarPoint.Web.Spread.NamedStyle("SearchMatch");
      sSearchMatch.BackColor = Color.LightSteelBlue;
      //Define a border
      FarPoint.Web.Spread.Border border = new FarPoint.Web.Spread.Border();
      border.BorderColor = Color.Black;
      border.BorderStyle = BorderStyle.Solid;
      border.BorderSize = 2;
      sSearchMatch.Border = border;

      //Apply the style
      FpSpread1.NamedStyles.Add(sSearchMatch);
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.FpSpread1.ButtonCommand += new FarPoint.Web.Spread.SpreadCommandEventHandler(this.FpSpread1_ButtonCommand);

    }
    #endregion

    private void FpSpread1_ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
      //Searching

      if (e.CommandName.StartsWith("search"))
      {

        //Get the search string
        int n = e.CommandName.IndexOf(".") + 1;
        string searchStr = e.CommandName.Substring(n);

        //Nothing entered
        if (searchStr.Trim() == "")
          return;

        //Clear the style model to remove any previous applied styles
        ((FarPoint.Web.Spread.Model.IRangeSupport)FpSpread1.Sheets[0].StyleModel).Clear(-1, -1, -1, -1);
        //Reload the skin
        //FarPoint.Web.Spread.DefaultSkins.GetAt(13).Apply(FpSpread1.Sheets[0]);
        //FpSpread1.CommandBar.BackColor = Color.Wheat;


        //Get search options
        bool caseSensitive = ckOptions.Items[0].Selected;
        bool exactMatch = ckOptions.Items[1].Selected;
        bool useWildcards = ckOptions.Items[2].Selected;
        bool alternateSearch = false;

        //Init vars
        int startRow = 0;
        int startColumn = 0;
        int rowIndex = 0;
        int colIndex = 0;
        string ret = "";

        int rowCnt = FpSpread1.ActiveSheetView.RowCount - 1;
        int colCnt = FpSpread1.ActiveSheetView.ColumnCount - 1;


        //Do Search
        //Loop through all the search matches
        while (ret != null)
        {
          //  returns the matching row(rowIndex) and col number(colIndex) if found
          //  returns -1 if not found
          ret = FpSpread1.Search(FpSpread1.ActiveSheetViewIndex, searchStr, caseSensitive, exactMatch, alternateSearch, useWildcards, startRow, startColumn, ref rowIndex, ref colIndex);

          //See if found a match, ie: none found or at end of search
          if ((ret != null) && (ret.Length > 0))
          {
            //Found a match.
            //Apply the NamedStyle (defined in page_load event)
            FpSpread1.ActiveSheetView.Cells[rowIndex, colIndex].StyleName = "SearchMatch";
            FpSpread1.ActiveSheetView.Cells[rowIndex, colIndex].Note = "Search match";
          }

          //Increment the starting row/and or column
          startColumn = colIndex + 1;
          if (startColumn > colCnt)
          {
            //Move to the next row, first col to continue the search
            startRow = rowIndex + 1;
            startColumn = 0;
          }
          else
            startRow = rowIndex;
        }
      }
    }
   
  }
}